package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PowerTreasure;
import com.ruoyi.system.service.IPowerTreasureService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 充电宝Controller
 * 
 * @author ruoyi
 * @date 2025-03-21
 */
@RestController
@RequestMapping("/system/treasure")
public class PowerTreasureController extends BaseController
{
    @Autowired
    private IPowerTreasureService powerTreasureService;

    /**
     * 查询充电宝列表
     */
    @PreAuthorize("@ss.hasPermi('system:treasure:list')")
    @GetMapping("/list")
    public TableDataInfo list(PowerTreasure powerTreasure)
    {
        startPage();
        List<PowerTreasure> list;
        // 判断用户是否为管理员
        if (SecurityUtils.getLoginUser().getUser().isAdmin()) {
            // 管理员查询所有充电宝
            list = powerTreasureService.selectPowerTreasureList(powerTreasure);
        } else {
            // 普通角色查询可用的充电宝
            powerTreasure.setAvailableStatus(1L);
            list = powerTreasureService.selectPowerTreasureList(powerTreasure);
        }
        return getDataTable(list);
    }

    /**
     * 导出充电宝列表
     */
    @PreAuthorize("@ss.hasPermi('system:treasure:export')")
    @Log(title = "充电宝", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PowerTreasure powerTreasure)
    {
        List<PowerTreasure> list = powerTreasureService.selectPowerTreasureList(powerTreasure);
        ExcelUtil<PowerTreasure> util = new ExcelUtil<PowerTreasure>(PowerTreasure.class);
        util.exportExcel(response, list, "充电宝数据");
    }

    /**
     * 获取充电宝详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:treasure:query')")
    @GetMapping(value = "/{treasureId}")
    public AjaxResult getInfo(@PathVariable("treasureId") Long treasureId)
    {
        return success(powerTreasureService.selectPowerTreasureByTreasureId(treasureId));
    }

    /**
     * 新增充电宝
     */
    @PreAuthorize("@ss.hasPermi('system:treasure:add')")
    @Log(title = "充电宝", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PowerTreasure powerTreasure)
    {
        return toAjax(powerTreasureService.insertPowerTreasure(powerTreasure));
    }

    /**
     * 修改充电宝
     */
    @PreAuthorize("@ss.hasPermi('system:treasure:edit')")
    @Log(title = "充电宝", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PowerTreasure powerTreasure)
    {
        return toAjax(powerTreasureService.updatePowerTreasure(powerTreasure));
    }

    /**
     * 删除充电宝
     */
    @PreAuthorize("@ss.hasPermi('system:treasure:remove')")
    @Log(title = "充电宝", businessType = BusinessType.DELETE)
	@DeleteMapping("/{treasureIds}")
    public AjaxResult remove(@PathVariable Long[] treasureIds)
    {
        return toAjax(powerTreasureService.deletePowerTreasureByTreasureIds(treasureIds));
    }
}
